/**
 * @author zj
 * @desc:
 * @date: 2024/8/11 11
 */
import { defineStore } from "pinia";

const useToken = defineStore("token", {
  state: () => {
    return {
      token: "",
      refreshToken: "",
    };
  },
  actions: {
    reset() {
      this.$reset();
      try {
        sessionStorage.removeItem("token");
        sessionStorage.removeItem("refreshToken");
      } catch (e) {
        console.warn("Failed to remove tokens from sessionStorage:", e);
      }
    },
    setToken(token) {
      if (typeof token !== "string") {
        console.error("Invalid token type, expected string");
        return;
      }
      this.token = token;
    },
    setRefreshToken(refreshToken) {
      if (typeof refreshToken !== "string") {
        console.error("Invalid refreshToken type, expected string");
        return;
      }
      this.refreshToken = refreshToken;
    },
    getToken() {
      return this.token;
    },
    getRefreshToken() {
      return this.refreshToken;
    },
  },
  persist: {
    enabled: true,
    storage: sessionStorage,
  },
});

export default useToken;
